﻿Public Class UserSettings
    Inherits System.Web.UI.Page

    Protected Property CurrentRecord As ObjectId

        Get

            If ViewState("CurrentRecord") Is Nothing Then

                ViewState("CurrentRecord") = ObjectId.Empty

            End If

            Return ViewState("CurrentRecord")

        End Get

        Set(ByVal value As ObjectId)

            ViewState("CurrentRecord") = value

        End Set

    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            If Not IsPostBack Then

                If Not HttpContext.Current.Session("User_id") Is Nothing Then

                    Me.CurrentRecord = HttpContext.Current.Session("User_id")
                    'HttpContext.Current.Session("MapID") = Nothing

                    LoadUserSettings()

                End If

            End If

            'If Facebook is enabled then show the deactivate button
            If DatabaseCalls.IsFacebookUser(Me.CurrentRecord) Then

                cmdConnectFacebook.Visible = False
                cmdDeactivateFacebook.Visible = True
                'chkAllowFacebookNotifications.Enabled = True

            Else
                'If facebook is not enabled then show the "Connect to Facebook" button.

                cmdConnectFacebook.Visible = True
                cmdDeactivateFacebook.Visible = False
                'chkAllowFacebookNotifications.Enabled = False

            End If

        Catch ex As Exception

        End Try

    End Sub

    Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click

        Try

            Call SaveUserSettings()

        Catch ex As Exception

        End Try

    End Sub

    Protected Sub ValidateData(ByRef bolValid As Boolean)

        Try

            'Public Property Password As String = ""
            'Public Property FacebookID As String = ""
            'Public Property FullName As String = ""
            'Public Property NickName As String = ""
            'Public Property JoinDate As Date = ""
            'Public Property InvitedBy As String = ""
            'Public Property AllowEmailNotifications As Boolean = True
            'Public Property AllowPhoneNotifications As Boolean = False
            'Public Property AllowFacebookNotifications As Boolean = False
            'Public Property Active As Boolean = True

            If txtEmail.Text.Trim = "" Then

                bolValid = False

                'Display Message

            End If

        Catch ex As Exception

        End Try

    End Sub

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        Try

            If HttpContext.Current.Session("User_id") = Nothing And HttpContext.Current.Session("Email") = Nothing Then

                Response.Redirect("Default.aspx")

            End If

        Catch ex As Exception

        End Try

    End Sub

    Protected Sub LoadUserSettings()

        Dim mUserSettings As Trebuchet.User = Nothing

        Try

            mUserSettings = DatabaseCalls.GetUserByID(Me.CurrentRecord)

            chkAllowEmailNotifications.Checked = mUserSettings.AllowEmailNotifications
            chkAllowFacebookNotifications.Checked = mUserSettings.AllowFacebookNotifications
            chkAllowPhoneNotifications.Checked = mUserSettings.AllowPhoneNotifications
            txtEmail.Text = mUserSettings.Email
            txtFullName.Text = mUserSettings.FullName
            txtNickName.Text = mUserSettings.NickName

            txtInvitedBy.Text = mUserSettings.InvitedBy
            ' txtJoinDate.Text = mUserSettings.JoinDate 
            'mUserSettings.Password
            ' txtFacebook.txt = mUserSettings.FacebookID 
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub cmdExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdExit.Click

        Try

            Response.Redirect("Dashboard.aspx")

        Catch ex As Exception

        End Try

    End Sub

    Private Sub cmdChangePassword_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdChangePassword.Click

        Dim mUserSettings As Trebuchet.User = Nothing
        Dim bolPasswordChanged As Boolean = False

        Try

            'Check if Current password validates 
            If DatabaseCalls.PasswordChanged(Me.CurrentRecord, txtChangePasswordCurrent.Text, txtChangePasswordNew.Text, txtChangePasswordNewConfirm.Text) Then

                bolPasswordChanged = True
                Call SaveUserSettings()

            Else

                bolPasswordChanged = False

            End If

        Catch ex As Exception

        End Try

    End Sub

    Private Sub SaveUserSettings()

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcUserSettings As MongoCollection(Of Trebuchet.User) = Nothing
        Dim mcUserSettingsCursor As MongoCursor = Nothing
        Dim mUserSettings As Trebuchet.User = Nothing
        Dim bolSave As Boolean = True

        Try

            'Validate Page Data
            ValidateData(bolSave)

            If bolSave Then

                'Check to ensure the user is in the system.
                mdDatabase = DBHelper.GetDataBase()
                mcUserSettings = mdDatabase.GetCollection(Of Trebuchet.User)("Users")

                mcUserSettingsCursor = mcUserSettings.FindAs(Of Trebuchet.User)(Query.And(Query.And(Query.EQ("_id", Me.CurrentRecord), Query.EQ("Active", True))))

                If Not mcUserSettingsCursor Is Nothing _
                    AndAlso mcUserSettingsCursor.Count > 0 Then

                    If mcUserSettingsCursor(0)._id <> Me.CurrentRecord Then

                        bolSave = False

                    End If

                End If

                If bolSave Then

                    'Update existing record.
                    mUserSettings = DatabaseCalls.GetUserByID(Me.CurrentRecord)

                    mUserSettings.AllowEmailNotifications = chkAllowEmailNotifications.Checked
                    mUserSettings.AllowFacebookNotifications = chkAllowFacebookNotifications.Checked
                    mUserSettings.AllowPhoneNotifications = chkAllowPhoneNotifications.Checked
                    mUserSettings.Email = txtEmail.Text
                    mUserSettings.FullName = txtFullName.Text
                    mUserSettings.NickName = txtNickName.Text
                    mUserSettings.InvitedBy = txtInvitedBy.Text
                    'mUserSettings.JoinDate = txtJoinDate.Text
                    'mUserSettings.Password
                    'mUserSettings.FacebookID = txtFacebook.txt
                End If

                mcUserSettings.Save(mUserSettings)

                Me.CurrentRecord = mUserSettings._id
                'LoadRecord()
                'Me.ScreenMode = enmScreenMode.Edit

            End If


        Catch ex As Exception

        End Try

    End Sub

End Class